package com.wn.study.view

import android.view.LayoutInflater
import android.view.ViewGroup
import androidx.paging.PagingDataAdapter
import androidx.recyclerview.widget.DiffUtil
import com.wn.common.widget.BindingViewHolder
import com.wn.study.databinding.ItemStudyBinding
import com.wn.study.repository.data.Daily2
import dagger.hilt.android.scopes.ActivityScoped
import javax.inject.Inject

/**
 * @Author: wn
 * @Date: 2023/7/17 16:16
 * @Description: 学习中心 paging3 适配器
 */
@ActivityScoped
class StudyAdapter @Inject constructor() :
  PagingDataAdapter<Daily2.Item, BindingViewHolder>(myDiff) {

  override fun onBindViewHolder(holder: BindingViewHolder, position: Int) {
    val item = getItem(position)
    item?.let {
      val binding = holder.binding as ItemStudyBinding
      binding.info = it
    }
  }

  /**
   * 多类型视图设置
   */
  /* override fun getItemViewType(position: Int): Int {
     return super.getItemViewType(position)
   }*/

  override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): BindingViewHolder {
    val binding = ItemStudyBinding.inflate(LayoutInflater.from(parent.context), parent, false)
    return BindingViewHolder(binding)
  }

}

// diff 比较
private val myDiff = object : DiffUtil.ItemCallback<Daily2.Item>() {
  override fun areItemsTheSame(oldItem: Daily2.Item, newItem: Daily2.Item): Boolean =
    oldItem.id == newItem.id

  override fun areContentsTheSame(oldItem: Daily2.Item, newItem: Daily2.Item): Boolean =
    oldItem == newItem
}